package ui.activity.profit;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import Bean.GetBankcardBean;
import Utils.ToastUtil;
import base.BaseAct;
import base.BaseBiz;
import butterknife.BindView;
import coom.RetrofitHelp;
import event.NetErrorEvent;
import ui.activity.mine.biz.BankCardBiz;
import ui.adapter.ChangebankAdapter;


/**
 * 修改默认银行卡
 * Created by admin on 2017/8/4.
 */

public class ChangeBankAct extends BaseAct  {
    BankCardBiz biz;
    List<GetBankcardBean.DataBean.ItemsBean> list = new ArrayList<>();
    private ChangebankAdapter adapter;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.submit_btn)
    TextView submit_btn;
    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_changebanksetting);
    }

    @Override
    protected void setup() {
        setToolBar("设置", "");
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        list= (List<GetBankcardBean.DataBean.ItemsBean>) getIntent().getSerializableExtra("data");
        recyclerView.setLayoutManager(new LinearLayoutManager(ChangeBankAct.this));
        adapter = new ChangebankAdapter(ChangeBankAct.this,list);
        recyclerView.setAdapter(adapter);
        biz = new BankCardBiz();
        RxView.clicks(submit_btn)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    submit();
                });
    }

    private void submit(){
        int changeposition = -1;
        for (int i= 0;i<list.size();i++){
            if(list.get(i).getIsChecked()){
                changeposition =i;
            }
        }
        if(changeposition == -1){
            ToastUtil.show(ChangeBankAct.this,"请选择一张银行卡！");
            return;
        }

        if(list.get(changeposition).getDefaultCard()==1){
            ToastUtil.show(ChangeBankAct.this,"当前选择银行卡已是默认银行卡！");
            return;
        }
        String url =  RetrofitHelp.BASE_URL + "api/card/default/"+list.get(changeposition).getId();
        biz.changeCard(url, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                ToastUtil.show(ChangeBankAct.this,"修改成功！");
                finish();
            }

            @Override
            public void onFailure(Object info) {


            }
        });
    }
    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }




}
